﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using TestWim.TestWimCL.POCO;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace TestWim.TestWimCL.DAL
{
  public class EFDbContext : DbContext
  {
    //No base-user scenario
    public DbSet<Student> Students { get; set; }

    public DbSet<Meeting> Meetings { get; set; }
    public DbSet<Team> Teams { get; set; }
    public DbSet<Event> Events { get; set; }
    public DbSet<Result> Results { get; set; }
    public DbSet<Record> Records { get; set; }

    //Base-user with generalization scenario
    public DbSet<User> Users { get; set; }
    public DbSet<Scc> Sccs { get; set; }
    public DbSet<School> Schools { get; set; }

    //User with intermediary relationships scenario
    public DbSet<Gebruiker> Gebruikers { get; set; }
    public DbSet<Nssf> Nssfs { get; set; }
    public DbSet<NssfRepresentation> NssfRepresentations { get; set; }
    public DbSet<Ploeg> Ploegs { get; set; }
    public DbSet<PloegMembership> PloegMemberships { get; set; }
    public DbSet<Resultaat> Resultaats { get; set; }

    public EFDbContext() : base("TestISFWim")
    {
      this.Configuration.LazyLoadingEnabled = true;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
      modelBuilder.Entity<Scc>().HasRequired(s => s.User).WithOptional(u => u.Scc);
      modelBuilder.Entity<School>().HasRequired(s => s.Scc).WithRequiredDependent(s => s.School);

      modelBuilder.Entity<NssfRepresentation>().HasRequired(nr => nr.Nssf).WithRequiredDependent(n => n.NssfRepresentation);
      modelBuilder.Entity<NssfRepresentation>().HasRequired(nr => nr.Gebruiker).WithOptional(u => u.NssfRepresentation);
    }
  }
}
